package zrpc

import (
	"znet/gorpc"
	"znet/glog"
	"errors"
)

type RpcServer struct {
	Addr string 			// 地址
	*gorpc.Dispatcher		// 派发器
	*gorpc.Server			// 服务器
}

func (this *RpcServer)Init(addr string,) {
	this.Addr 		= addr
	this.Dispatcher = gorpc.NewDispatcher()
}
func (this *RpcServer)Start() error{
	if this.Addr == "" {
		glog.Fatal("Start RPC server Error: ","Addr is nil")
		return errors.New("addr is nil")
	}
	if this.Dispatcher == nil {
		glog.Fatal("Start RPC server Error: ","Dispatcher is nil")
		return errors.New("dispatcher is nil")
	}
	// 创建rpc服务器
	this.Server = gorpc.NewTCPServer(this.Addr,this.Dispatcher.NewHandlerFunc())
	err := this.Server.Start()
	if err != nil {
		return err
	}
	glog.Info("Start RPC server Succeed: ",this.Addr)
	return nil
}
func (this *RpcServer) Stop() {
	// 关闭连接
	if this.Server != nil{
		this.Server.Stop()
		glog.Info("Stop RPC server Succeed: ",this.Addr)
	}
}
